package com.ysd.lis.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysd.common.Result;
import com.ysd.lis.entity.SysUser;
import com.ysd.lis.entity.TDeptGroup;
import com.ysd.lis.entity.TUserGroup;
import com.ysd.lis.mapper.sys.SysUserMapper;
import com.ysd.lis.mapper.TDeptGroupMapper;
import com.ysd.lis.mapper.TUserGroupMapper;
import com.ysd.lis.request.OrgDepartmentParam;
import com.ysd.lis.service.TDeptGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TDeptGroupServiceImpl extends ServiceImpl<TDeptGroupMapper, TDeptGroup> implements TDeptGroupService {
    @Autowired
    TDeptGroupMapper tDeptGroupMapper;
    @Autowired
    SysUserMapper sysUserMapper;
    @Autowired
    TUserGroupMapper tUserGroupMapper;
    @Override
    public Result addDeptGroup(TDeptGroup tDeptGroup) {
        int insert = tDeptGroupMapper.insert(tDeptGroup);
        if(insert>0){
            return Result.succ(1,"保存成功",tDeptGroup);
        }else{
            return Result.fail(400,"新增失败","");
        }
    }

    @Override
    public Result findDeptGroupListByDeptId(String deptId,String status) {
        LambdaQueryWrapper<TDeptGroup> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(TDeptGroup::getDelFlag,0).eq(TDeptGroup::getDeptId,deptId);
        if(StringUtils.isNotBlank(status)){
            lambdaQueryWrapper.eq(TDeptGroup::getStatus,status);
        }
        List<TDeptGroup> tDeptGroups = tDeptGroupMapper.selectList(lambdaQueryWrapper);
        if(CollectionUtils.isNotEmpty(tDeptGroups)){
            tDeptGroups.forEach(a->{
                SysUser sysUser = sysUserMapper.selectById(a.getGroupLeaderId());
                if(null != sysUser) a.setGroupLeaderName(sysUser.getRealname());
            });
        }
        return Result.succ(1,"查询成功",tDeptGroups);
    }

    @Override
    public Result editDeptGroup(TDeptGroup tDeptGroup) {
        int update = tDeptGroupMapper.updateById(tDeptGroup);
        if(update>0){
            return Result.succ(1,"修改成功",tDeptGroup);
        }else{
            return Result.fail(400,"修改失败","");
        }
    }

    @Override
    public Result deleteDeptGroup(String id) {
        //删除部分分组时先查看是否有人员在此组下
        LambdaQueryWrapper<TUserGroup> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TUserGroup::getGroupId,id).eq(TUserGroup::getDelFlag,'0');
        Integer selectCount = tUserGroupMapper.selectCount(queryWrapper);
        if(selectCount>0){
            return Result.fail(400,"删除失败,此组下存在用户","");

        }else{
            int delete = tDeptGroupMapper.deleteById(id);
            if(delete>0){
                return Result.succ(1,"删除成功",null);
            }else{
                return Result.fail(400,"删除失败","");
            }
        }
    }

    @Override
    public Result getDeptGroupListByOrgId(OrgDepartmentParam orgDepartmentParam) {
        List<TDeptGroup> deptGroupList = tDeptGroupMapper.getDeptGroupListByOrgId(orgDepartmentParam);
        return Result.succ(1,"删除成功",deptGroupList);
    }
}
